package com.link.base.base.wechat.controller;

import com.link.base.base.wechat.model.ImgGroup;
import com.link.base.base.wechat.model.WxImage;
import com.link.base.base.wechat.service.ImgGroupService;
import com.link.base.base.wechat.service.WxImageService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图片分组的基本操作
 *
 * @author lujiang
 * @date 2018-05
 */
@Controller
@RequestMapping("/link/imggroup")
public class ImgGroupController extends BasicController<ImgGroup> {
    @Resource
    private WxImageService wxImageService;
    @Resource
    private ImgGroupService imggroupService;

    @Override
    public BasicService<ImgGroup> getBasicService() throws Exception {
        return imggroupService;
    }

    /**
     * 重写查询方法,根据条件查询记录,默认分页(若将'pageFlag'设置为'false'，则不分页)
     * 查询图片分组的时候单独把全部分组，未分组的数量的查询一起返回前端
     *
     * @author lujiang
     * @date 2018-05
     * @param qps
     * @param entity 分组信息
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryByExamplePage")
    @ResponseBody
    @Override
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps,@JsonParam  ImgGroup entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<String, Object>(16);
        List<ImgGroup> imgGroupList = new ArrayList<>();
        try {
            qps.preDealSecurity(request);
            beforQueryAllOrExample(entity, request);
            beforQueryExample(entity, request);
            // 查询当前公众号下所有图片，数量，即是全部分组的数量
            WxImage image = new WxImage();
            image.setLoginCorpId(entity.getLoginCorpId());
            image.setWxId(entity.getWxId());
            image.setPageFlag(false);
            ImgGroup allImgGroup = new ImgGroup();
            allImgGroup.setId(1L);
            allImgGroup.setGroupName("全部分组");
            allImgGroup.setWxId(entity.getWxId());
            List<WxImage> images = wxImageService.queryByExamplePage(image);
            allImgGroup.setCount(images.size());
            // 查询当前公众号下未分组图片数量
            ImgGroup noImgGroup = new ImgGroup();
            noImgGroup.setId(2L);
            noImgGroup.setGroupName("未分组");
            noImgGroup.setWxId(entity.getWxId());
            image.setGroupId(2L);
            List<WxImage> listImage = wxImageService.queryByExamplePage(image);
            noImgGroup.setCount(listImage.size());
            imgGroupList.add(allImgGroup);
            imgGroupList.add(noImgGroup);
            List<ImgGroup> list = getBasicService().queryByExamplePage(entity);
            imgGroupList.addAll(list);
            for (ImgGroup imggroup :imgGroupList) {
                if (imggroup.getCount() < 0 ) {
                    imggroup.setCount(0);
                }
            }
            result.put("success", true);
            result.put("rows", imgGroupList);
            this.pushCurrentDataCache(imgGroupList, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) entity, imgGroupList.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<ImgGroup>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @Override
    public void beforUpsert(ImgGroup entity, HttpServletRequest request) throws Exception {
        ImgGroup imggroup = new ImgGroup();
        imggroup.setWxId(entity.getWxId());
        imggroup.setGroupName(entity.getGroupName());
        imggroup.setPageFlag(false);
        if (imggroupService.queryByExamplePage(imggroup).size() > 0) {
            throw new ServiceException("WECHAT-001", entity.getGroupName());
        }
    }

    /**
     * 重写删除
     * 删除分组的时候将此分组下的图片的分组信息设置为未分组
     *
     * @author lujiang
     * @date 2018-05
     */
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    @Override
    public Map<String, Object> deleteById(@JsonParam ImgGroup entity, HttpSession session, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map = super.deleteById(entity, session, request, response);
        String trueFlag = "true";
        String suc = "success";
        if (trueFlag.equals(map.get(suc).toString())) {
            WxImage image = new WxImage();
            image.setGroupId(entity.getId());
            image.setPageFlag(false);
            image.setLoginCorpId(entity.getLoginCorpId());
            List<WxImage> images;
            try {
                images = wxImageService.queryByExamplePage(image);
                // 删除分组的时候将此分组下的图片的分组信息设置为未分组
                for (int i = 0; i < images.size(); i++) {
                    WxImage newImage = images.get(i);
                    newImage.setGroupId(00002L);
                    //不改变原有图片的路径
                    newImage.setTempMaterialUrl("");
                    wxImageService.update(newImage);
                }
            } catch (Exception e) {
                map.put("success", false);
                LCLogger.withException(e);
            }
        }
        return map;
    }
}
